﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Diagnostics;
using System.Net;
using OL2012Mayhem.Events;

namespace OL2012Mayhem.Helpers
{
    public class CountryEntry
    {
        public string Url { get; set; }
        public string Country { get; set; }
    }

    public class CollectDataHelper
    {
        public static List<CountryEntry> Countries = null;

        public static List<CountryEntry> GetCountries()
        {
            Countries = new List<CountryEntry>();
            var wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5";
            try
            {
                var html = wc.DownloadString(new Uri("http://en.wikipedia.org/wiki/2012_Summer_Olympics"));
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);

                var table = doc.DocumentNode.SelectNodes("//table[.//li/a[text() = 'Afghanistan'] and .//li/a[text() = 'Denmark']]").FirstOrDefault();

                var entries = table.SelectNodes(".//li");

                foreach (var entry in entries)
                {
                    var url = "http://en.wikipedia.org" + entry.SelectSingleNode("a").Attributes["href"].Value;
                    var name = HtmlEntity.DeEntitize(entry.SelectSingleNode("a").InnerText);

                    Countries.Add(new CountryEntry() { Url = url, Country = name });
                }

                Console.WriteLine("Done");
            }
            catch (Exception e)
            {
            }
            return Countries;
        }

        public static int GetMedalCount(string countryUrl, string medalType)
        {
            var wc = new WebClient();
            wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5";
            try
            {
                var html = wc.DownloadString(new Uri(countryUrl));

                //Find the medals
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);

                var table = doc.DocumentNode.SelectNodes("//table[contains(@class,'infobox')]").FirstOrDefault();

                if (table == null)
                {
                    return 0;
                }

                var gold = table.SelectNodes("//td/b[text() = 'Gold']/following-sibling::span").FirstOrDefault();
                var silver = table.SelectNodes("//td/b[text() = 'Silver']/following-sibling::span").FirstOrDefault();
                var bronze = table.SelectNodes("//td/b[text() = 'Bronze']/following-sibling::span").FirstOrDefault();

                var goldNumber = GetMedals(gold);
                var silverNumber = GetMedals(silver);
                var bronzeNumber = GetMedals(bronze);

                if(medalType.Equals(MedalTypes.AllMedals))
                {
                    return goldNumber + silverNumber + bronzeNumber;
                }
                else if (medalType.Equals(MedalTypes.GoldMedals))
                {
                    return goldNumber;
                }
                else if (medalType.Equals(MedalTypes.SilverMedals))
                {
                    return silverNumber;
                }
                else if (medalType.Equals(MedalTypes.BronzeMedals))
                {
                    return bronzeNumber;
                }

            }
            catch (Exception e)
            {
            }
            return 0;
        }


        private static int GetMedals(HtmlNode text)
        {
            var number = -1;
            if (text != null)
            {
                int.TryParse(text.InnerText, out number);
            }
            return number;
        }

    }
}
